![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Bug Proofing Visual Basic: A Guide to Error Handling and Prevention
The following code shows how program Bad5 works. Appendix A, Self-Test Solutions, contains one possible improved version of this code. Remember that this is not the only possible solution. You can download updated bad and good versions of this program from the Web at
Option Explicit
Declare the list of values.
Const NUM_VALUES = 50
Private Values(1 To NUM_VALUES) As Integer
Randomly initialize some values.
Private Sub Form_Load()
InitValues
End Sub
Randomly initialize some values.
Private Sub InitValues()
Dim i As Integer
Dim txt As String
Pick the random values.
Randomize
Values(1) = Int(10 * Rnd + 1)
For i = 2 To NUM_VALUES
Values(i) = Values(i - 1) + Int(10 * Rnd + 1)
Next i
Display the values.
For i = 1 To NUM_VALUES
txt = txt & Format$(i, @@) & _
Format$(Values(i), @@@@) & vbCrLf
Next i
txtValues.Text = txt
End Sub
Search using the selected method.
Private Sub CmdSearch_Click()
Dim position As Integer
Dim num_searches As Integer
If optSearchMethod(0).Value Then
Linear search.
position = LinearSearch(Values, _
CInt(txtTarget.Text), num_searches)
ElseIf optSearchMethod(1).Value Then
Binary search.
position = BinarySearch(Values, _
CInt(txtTarget.Text), num_searches)
End If
Display the results.
If position = 0 Then
lblPosition.Caption = Not found
Else
lblPosition.Caption = Format$(position)
End If
lblSearches.Caption = Format$(num_searches)
End Sub
Use linear binary search to find the target.
Public Function LinearSearch(list() As Integer, _
target As Integer, num_searches As Integer) As Integer
Dim i As Integer
For i = 1 To NUM_VALUES
num_searches = num_searches + 1
If Values(i) = target Then Exit For
Next i
If i <= NUM_VALUES Then
LinearSearch = i
Else
LinearSearch = 0
End If
End Function
Use binary search to find the target. For details
of the algorithm, see Ready-to-Run Visual Basic
Algorithms, ISBN 0-471-24268-3.
Public Function BinarySearch(list() As Integer, _
target As Integer, num_searches As Integer) As Integer
Dim min_index As Integer
Dim max_index As Integer
Dim mid_index As Integer
min_index = 1
max_index = NUM_VALUES
Do While min_index <= max_index
num_searches = num_searches + 1
mid_index = (min_index + max_index) / 2
If target = list(mid_index) Then
BinarySearch = mid_index
Exit Function
ElseIf target < list(mid_index) Then
max_index = mid_index - 1
Else
min_index = mid_index + 1
End If
Loop
BinarySearch = 0
End Function
SummaryFinding bugs can be easy or hardthe choice is yours. If you write code that quietly ignores data that makes no sense, it will hide bugs so finding them will be difficult. If you write code that jumps up and shouts, Theres a bug here, whenever it sees a suspicious value, the bugs will come to you. This does not excuse you from testing the code, but it can reduce the number of bugs you need to find during testing. The following Bug Stoppers list the key bug-exposing concepts described in this chapter.
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|